package com.zzxx.controller;

import com.zzxx.entity.ActivityContent;
import com.zzxx.entity.GymContent;
import com.zzxx.entity.User;
import com.zzxx.entity.UserContent;
import com.zzxx.service.ContentService;
import com.zzxx.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("content")
public class ContentController {
    @Autowired
    private ContentService contentService;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private OrderService orderService;
    @RequestMapping("loadList")
    public List<GymContent> loadList(String id){
        List<GymContent>  list=contentService.list(id);
        return list;
    }

    @RequestMapping("loadActivityList")
    public List<ActivityContent> loadActivityList(String id){
        List<ActivityContent>  list=contentService.list2(id);
        return list;
    }
  @RequestMapping("addlike")
  public String addlike(String cid){
      User loginUser =(User) request.getSession().getAttribute("loginUser");
      String uid=loginUser.getId();
      contentService.addlike(cid,uid);
        return "ok";
  }
  @RequestMapping("findlike")
  public List<UserContent> findlike(){
      User loginUser =(User) request.getSession().getAttribute("loginUser");
      if(loginUser==null) return null;
      String uid=loginUser.getId();
    List<UserContent> list=contentService.findlike(uid);
    return list;
  }
  @RequestMapping("deletelike")
  public String deletelike(String cid){
      User loginUser =(User) request.getSession().getAttribute("loginUser");
      String uid=loginUser.getId();
        contentService.deletelike(cid,uid);
        return "ok";
  }
  @RequestMapping("send")
  public String send(@RequestBody GymContent gymContent){
      User loginUser =(User) request.getSession().getAttribute("loginUser");
      String uid=loginUser.getId();
      gymContent.setUserid(uid);
      contentService.send(gymContent);
        return "ok";
  }
  @RequestMapping("deleteContent")
  public String deleteContent(String cid){
        contentService.deleteContent(cid);
        return  "ok";
  }
  @RequestMapping("findOne")
  public GymContent findOne(String cid){
     GymContent gymContent =  contentService.findOne(cid);
        return gymContent;
  }
  @RequestMapping("updateContent")
  public String updateContent(@RequestBody GymContent gymContent){
     contentService.updateContent(gymContent);
     return "ok";
  }

    @RequestMapping("sendA")
    public String sendA(@RequestBody ActivityContent activityContent){
        User loginUser =(User) request.getSession().getAttribute("loginUser");
        String uid=loginUser.getId();
        activityContent.setUserid(uid);
        contentService.sendA(activityContent);
        return "ok";
    }

    @RequestMapping("addAClike")
    public String addAClike(String cid){
        User loginUser =(User) request.getSession().getAttribute("loginUser");
        String uid=loginUser.getId();
        contentService.addAClike(cid,uid);
        return "ok";
    }

    @RequestMapping("deleteAClike")
    public String deleteAClike(String cid){
        User loginUser =(User) request.getSession().getAttribute("loginUser");
        String uid=loginUser.getId();
        contentService.deleteAClike(cid,uid);
        return "ok";
    }

    @RequestMapping("deleteACContent")
    public String deleteACContent(String cid){
        contentService.deleteACContent(cid);
        return  "ok";
    }

    @RequestMapping("updateACContent")
    public String updateACContent(@RequestBody ActivityContent activityContent){
        contentService.updateACContent(activityContent);
        return "ok";
    }

    @RequestMapping("findACOne")
    public ActivityContent findACOne(String cid){
        ActivityContent activityContent =  contentService.findACOne(cid);
        return activityContent;
    }
  @RequestMapping("updatelike1")
 public String updatelike1(String cid){
     request.getSession().setAttribute("cid",cid);
      return "OK";
  }
    @RequestMapping("updatelike2")
    public List<GymContent> updatelike2(@RequestBody List<GymContent> gymContents){
        String cid = (String) request.getSession().getAttribute("cid");
      for(GymContent g:gymContents){
          if(g.getId().equals(cid)){
              g.setIslike(true);
          }
      }
        return gymContents;
    }
    @RequestMapping("updateType")
    public String updateType(String oid){
        orderService.updateType(oid);
        return "ok";
    }
}
